package com.handler.inbound;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.to.FileReq;
import com.to.MetaInfo;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelOutboundHandler;
import io.netty.channel.ChannelPromise;

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.net.SocketAddress;
import java.net.URI;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class MetaInfoDecoder extends ChannelInboundHandlerAdapter {

    private ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

        ByteBuf buf = (ByteBuf) msg;

        int metaInfoLength = buf.readInt();

        byte[] metaInfoBytes = new byte[metaInfoLength];

        buf.readBytes(metaInfoBytes, 0, metaInfoLength);

        MetaInfo metaInfo = objectMapper.readValue(new String(metaInfoBytes, StandardCharsets.UTF_8), MetaInfo.class);

        ctx.fireChannelRead( new FileReq(metaInfo, buf));

    }


}
